/**
 * 
 */
package org.kopsox.validation.impl;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.util.List;

import org.kopsox.comm.message.ErrorMessage;

/**
 * This Exception is thrown by the AbstractVetoableValidator if the method vetoableChange was called.<br>
 * The Exception is just thrown if the Valdation-process was not OK!<br>
 * 
 * @author Konrad Renner - 07.12.2010
 */
public class KopsoxVetoException extends PropertyVetoException {
	
	private static final long serialVersionUID = -2917951212288807528L;
	
	private final List<ErrorMessage> errorMessages;

	public KopsoxVetoException(List<ErrorMessage> errors,PropertyChangeEvent event) {
		super(createMessageString(errors),event);
		this.errorMessages = errors;
	}
	
	public List<ErrorMessage> getErrorMessages(){
		return this.errorMessages;
	}
	
	private final static String createMessageString(List<ErrorMessage> messages) {
		
		StringBuilder sb = new StringBuilder();
		for(ErrorMessage m : messages) {
			if(m.getNameOfErrorSource() != null) {
				sb.append(m.getNameOfErrorSource());
				sb.append(':');
			}
			sb.append(m.getMessage());
			sb.append(';');
			sb.append('\n');
		}
		
		return sb.toString();
	}
}
